package top.continew.admin.jobfair.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;
import top.continew.admin.jobfair.mapper.CompanyMapper;
import top.continew.admin.jobfair.model.entity.CompanyDO;
import top.continew.admin.jobfair.model.query.CompanyQuery;
import top.continew.admin.jobfair.model.req.CompanyReq;
import top.continew.admin.jobfair.model.resp.CompanyDetailResp;
import top.continew.admin.jobfair.model.resp.CompanyResp;
import top.continew.admin.jobfair.service.CompanyService;

import java.util.List;

/**
 * 企业信息管理业务实现
 *
 * @author xiahaike
 * @since 2024/09/10 20:58
 */
@Service
@RequiredArgsConstructor
public class CompanyServiceImpl extends BaseServiceImpl<CompanyMapper, CompanyDO, CompanyResp, CompanyDetailResp, CompanyQuery, CompanyReq> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public CompanyResp selectOne(CompanyQuery companyQuery) {
        QueryWrapper<CompanyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_user", companyQuery.getCreateUser());
        CompanyDO companyDO = companyMapper.selectOne(queryWrapper);
        if (companyDO == null) {
            return null;
        }
        // 将 CompanyDO 转换为 CompanyResp
        CompanyResp companyResp = new CompanyResp();
        BeanUtil.copyProperties(companyDO, companyResp);
        return companyResp;
    }

    @Override
    public PageResp<CompanyResp> listByIds(List<Long> companyIds) {
        List<CompanyDO> companyDOList = companyMapper.selectBatchIds(companyIds);
        if (companyDOList != null && !companyDOList.isEmpty()) {
            List<CompanyResp> companyRespList = BeanUtil.copyToList(companyDOList, CompanyResp.class);
            // 将List<CompanyDO> 转换为 PageResp<CompanyResp>
            PageResp<CompanyResp> pageResp = new PageResp<>();
            pageResp.setList(companyRespList);
            return pageResp;
        }
        return null;
    }

    @Override
    public boolean checkDuplicate(CompanyReq req, Long id) {
        QueryWrapper<CompanyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_user", id);
        return this.baseMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public Long getCompanyId(Long id) {
        QueryWrapper<CompanyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_user", id);
        CompanyDO companyDO = companyMapper.selectOne(queryWrapper);
        if (companyDO == null) {
            return null;
        }
        return companyDO.getId();
    }
}